
/*
 * @Descripttion: 
 * @Author: yang fu ren
 * @version: 
 * @Date: 2025-06-23 09:11:35
 * @LastEditors: yang fu ren
 * @LastEditTime: 2025-06-23 15:53:11
 */

import { createRouter, createWebHistory } from 'vue-router'

// 使用绝对路径导入（确保Vite别名配置正确）
const staticRoutes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/LoginView.vue'), // 使用@别名
    meta: { title: '登录' } // 建议添加标题
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('@/components/work_01.vue'), // 使用@别名
    meta: { title: '首页' }, // 建议添加标题
    children: [
      {
        path: '/app-management',
        name: 'AppManagement',
        component: () => import('@/views/APPManagement.vue'), // 使用@别名
        meta: { title: 'APP管理' }
      },
      {
        path: '/user-app',
        name: 'UserApp',
        component: () => import('@/views/UserApp.vue'), // 统一组件命名
        meta: { title: '用户管理' }
      },
      {
        path: '/screen-config',
        name: 'LargeScreenConfiguration',
        component: () => import('@/views/LargeScreenConfiguration.vue'), // 统一组件命名
        meta: { title: '大屏匹配' }
      },
        {
        path: '/datasource',
        name: 'DataSourceManagement',
        component: () => import('@/views/DataSourceManagement.vue'), // 统一组件命名
        meta: { title: '数据源管理' }
      },
      {
        path: '/templates',
        name: 'TemplateDownload',
        component: () => import('@/views/TemplateDownload.vue'), // 统一组件命名
        meta: { title: '模版下载' }
      },
      {
        path: '/:pathMatch(.*)*', // 捕获所有未匹配路径
        name: 'NotFound',
        component: () => import('@/views/NotFound.vue'),
        meta: { title: '页面不存在' }
      },
      
  {
    path: '/user/list',
    name: 'user-list',
    component: () => import('@/views/UserInfo.vue'), // 使用@别名
    meta: { title: '用户列表' }
      },
      
    ]
  },
  
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: staticRoutes
})

// 动态路由加载函数
export const setupDynamicRoutes = async () => {
  try {
    const { useMenuStore } = await import('@/stores/menu')
    const menuStore = useMenuStore()
    
    if (menuStore.menus.length === 0) {
      await menuStore.fetchMenus()
    }

    menuStore.menus.forEach(menu => {
      if (!router.hasRoute(menu.path)) {
        router.addRoute({
          path: menu.path,
          name: `menu-${menu.id}`,
          component: () => import(/* @vite-ignore */ `@/views/${menu.component || `Menu${menu.id}`}.vue`),
          meta: { title: menu.title }
        })
      }
    })
  } catch (error) {
    console.error('动态路由加载失败:', error)
  }
}

export default router